//
// Created by Bili_TianX_Computer on 2021/7/8.
//

#include "UserPanel.h"

UserPanel::UserPanel()
{
    formLayout = new QFormLayout();
    widgetLayout = new QVBoxLayout();
    loginButton = new QPushButton("登录");
    accountEdit = new QLineEdit();
    passwordEdit = new QLineEdit();

    formLayout->addRow("用户名", accountEdit);
    formLayout->addRow("密码", passwordEdit);
    widgetLayout->addLayout(formLayout);
    widgetLayout->addWidget(loginButton);

    setWindowTitle("小型超市管理系统");
    setLayout(widgetLayout);

    passwordEdit->setEchoMode(QLineEdit::Password);

    connect(loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClickedSlot()));
}

void UserPanel::loginButtonClickedSlot()
{
    User *user = queryUserByAccount(accountEdit->text());

    if (user == nullptr)
        QMessageBox::critical(this, "错误", "用户不存在");
    else if (user->password != passwordEdit->text())
        QMessageBox::critical(this, "错误", "密码不正确");
    else
    {
        QMessageBox::information(this, "登录成功", QString("欢迎%1使用本系统！").arg(user->name));
        hide();
        emit logined(user->isAdmin);
    }
}

